package cn.middle.application.portal;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.middle.common.result.ApiResponse;
import com.middle.common.result.Page;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.middle.application.dto.RefreshEdgeDto;
import cn.middle.application.entity.Park;
import cn.middle.application.service.ParkService;
import cn.middle.remote.user.UserClient;

/**
 * 园区列表
 * @author KING
 *
 */
@Controller
@RequestMapping("/park")
public class ParkController {
	
	private final static String PREFIX = "edge/park/";
	
	@Autowired
	private UserClient client;
	
	@Autowired
	private ParkService parkService;
	@SaCheckPermission("park:list")
	@GetMapping("/list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer limit, String parkCode,
			String parkName, ModelMap modelMap) {
		Page<Park> pageInfo = parkService.listConditions(pageNum, limit, parkCode, parkName);
		modelMap.put("pageInfo", pageInfo);
		modelMap.put("parkCode", parkCode);
		modelMap.put("parkName", parkName);
		return PREFIX + "list";
	}
	
	@ResponseBody
	@SaCheckPermission("park:synchronous")
	@SuppressWarnings("unchecked")
	@GetMapping("/synchronous")
	public ApiResponse<Boolean> synchronous(Byte synchronousType,String parkCode){
		
		RefreshEdgeDto dto = new RefreshEdgeDto();
		List<String> parkCodes = new ArrayList<String>();
		dto.setParkCodes(parkCodes);
		if(synchronousType == 0) { //同步全部
			dto.setRefreshType(Byte.parseByte("0"));
		}else { //根据园区ID同步
			if(parkCode == null) {
				return ApiResponse.FAIL_WITH_PARAME_MISSING;
			}
			dto.setRefreshType(Byte.parseByte("1"));
			parkCodes.add(parkCode);		
		}
		this.client.refreshEdge(UUID.randomUUID().toString(), dto);
		return ApiResponse.successWithData(true);
	}

}
